<?php

namespace App\Http\Controllers\Admin\Auth;

use App\Http\Models\Admin\Auth\RoleResource;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Responses\ResponseDto;
use App\Http\Models\Admin\Auth\Resource;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\View;
use Psy\Util\Json;

class ResourceController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //
        //
        //
        return View::make('admin.auth.resource')->withUserinfo($request->session()->get('adminUser'))
            ->withUrltitle(trans("admin/message.auth_resource"))
            ->withAssets($request['ASSETS']);
    }

    public function page(Request $request)
    {
        $datas = $request->input();
        $start = $datas[3]['value'];
        $length = $datas[4]['value'];

        $users = DB::table('resource')->skip($start)->take($length)->get();
        $total = DB::table('resource')->count();
        return ResponseDto::createResObj(ResponseDto::convertPageToDataTableObj($users,$total),ResponseDto::$SUCCESS);
    }
    public function create()
    {
        //
        $list = Resource::whereNull('parent_id')->orWhere('parent_id',0)->orWhere('parent_id',"")->orderBy('order_index','asc')->get();
        foreach($list as $item){
            $this->inTreeData($item);
        }
        return Json::encode($list);
    }

    public function inTreeData($data)
    {
        //
        $data->children =  Resource::where('parent_id',$data->id)->orderBy('order_index','asc')->get();

        if($data->children == null){
            return;
        }
        foreach($data->children as $child){
            $this->inTreeData($child);
        }

    }
    public function updateCtrees(Request $request)
    {
        $data_src = $request->input("data");
        $datas = json_decode($data_src);
        Resource::whereNotNull('id')->update(['parent_id'=>0]);

        foreach($datas as $k=>$data){
            Resource::where('id', $data->id)->update(['parent_id'=>0,'order_index'=>$k]);
            $this->updateCtreesItem($data);
        }
        return ResponseDto::createResObj(trans("admin/message.update_success"),ResponseDto::$SUCCESS);
    }
    public function updateCtreesItem($item)
    {
        if(property_exists($item,'children')) {
            foreach($item->children as $k=>$citem){
                Resource::where('id', $citem->id)->update(['parent_id'=>$item->id,'order_index'=>$k]);
                $this->updateCtreesItem($citem);
            }
        }
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

        $validator = Validator::make(Input::all(),
            [
                'name' => 'required',
                'res_url' => 'required',
            ]);

        if ($validator->fails()) {
            return ResponseDto::createResObj($validator->errors()->first(), ResponseDto::$FAIL);
        } else {
            $resource = new Resource();
            $resource->name = $request->input("name");
            $resource->r_icon = $request->input("r_icon");
            $resource->res_url = $request->input("res_url");
            $resource->parent_id = $request->input("parent_id");

            if ($request->input("id")) {
                //更新操作
                $exit_item = Resource::where('id', '<>', $request->input("id"))->where('name', $resource->name)->first();
                if ($exit_item) {
                    return ResponseDto::createResObj(trans("admin/message.name_exit"), ResponseDto::$FAIL);
                } else {
                    $up_item = Resource::find($request->input("id"));
                    $up_item->name = $resource->name;
                    $up_item->r_icon = $resource->r_icon;
                    $up_item->res_url = $resource->res_url;
                    $up_item->parent_id = $resource->parent_id;
                    if ($up_item->save()) {
                        return ResponseDto::createResObj(trans("admin/message.update_success"), ResponseDto::$SUCCESS);
                    } else {
                        return ResponseDto::createResObj(trans("admin/message.service_fail"), ResponseDto::$FAIL);
                    }
                }
            } else {
                //添加操作
                $find = Resource::where('name', $request->input("name"))->first();
                if ($find) {
                    return ResponseDto::createResObj(trans("admin/message.name_exit"), ResponseDto::$FAIL);
                }
                if ($resource->save()) {
                    return ResponseDto::createResObj(trans("admin/message.submit_success"), ResponseDto::$SUCCESS);
                } else {
                    return ResponseDto::createResObj(trans("admin/message.service_fail"), ResponseDto::$FAIL);
                }
            }


        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
        $find =Resource::find($id);
        if($find && $find->parent_id){
            $parent = Resource::find($find->parent_id);
            if($parent){
                $find->parent_name = $parent->name;
            }
        }
        return Json::encode($find);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        Resource::destroy($id);
//        PostTags::where('tag_id',$id)->delete();

        RoleResource::where('res_id',$id)->delete();
        return ResponseDto::createResObj(trans("admin/message.update_success"),ResponseDto::$SUCCESS);
    }
}
